//
//  SYBaiBanTopView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/12/27.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYBaiBanTopView.h"
#import "SYBaiBanTopCollectionViewCell.h"
#import "SYBaiBanModel.h"

@interface SYBaiBanTopView () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) UICollectionView *curCollectionView;

@property (assign, nonatomic) NSInteger index;

@end

@implementation SYBaiBanTopView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initSubView];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)initSubView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat w = 70;
    CGFloat h = 30;
    flowLayout.itemSize = CGSizeMake(w , h );
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _curCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 50) collectionViewLayout:flowLayout];
//    [self.curCollectionView setCollectionViewLayout:flowLayout];
    self.curCollectionView.backgroundColor = [UIColor whiteColor];
    self.curCollectionView.delegate = self;
    self.curCollectionView.dataSource = self;
    [self addSubview:_curCollectionView];
    [_curCollectionView registerNib:[UINib nibWithNibName:@"SYBaiBanTopCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SYBaiBanTopCollectionViewCell"];
    [_curCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
    
}

- (void)setDataListArr:(NSArray *)dataListArr {
    _dataListArr = dataListArr;
    [self.curCollectionView reloadData];
}


#pragma mark - dele
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataListArr.count;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    SYBaiBanTopCollectionViewCell *topCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SYBaiBanTopCollectionViewCell" forIndexPath:indexPath];
    SYBaiBanTopModel *topModel = self.dataListArr[indexPath.row];
    topCell.nameLab.text = topModel.titleName;
    if (topModel.isSelect == YES) {
        topCell.nameLab.textColor = [UIColor colorWithHexString:@"20b097"];
        self.index = indexPath.row;
    } else {
        topCell.nameLab.textColor = [UIColor blackColor];
    }
    
    return topCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_index == indexPath.row) {
        return;
    }
    SYBaiBanTopModel *selectTopModel = self.dataListArr[_index];
    selectTopModel.isSelect = NO;
    SYBaiBanTopModel *topModel = self.dataListArr[indexPath.row];
    topModel.isSelect = YES;
    if (self.block) {
        self.block(topModel.fileId);
    }
    [self.curCollectionView reloadData];
}
@end
